package com.fufufu.mail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fufufu.mail.product.vo.CategoryBrandRalationResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fufufu.mail.product.entity.CategoryBrandRelationEntity;
import com.fufufu.mail.product.service.CategoryBrandRelationService;
import com.fufufu.common.utils.PageUtils;
import com.fufufu.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author zyx
 * @email 1262074394@qq.com
 * @date 2022-09-15 08:15:48
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @GetMapping("/catelog/list")
    public R list(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("brand_id", brandId));
        return R.ok().put("data", data);
    }


    /**
     * 获取品牌关联的类别
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @GetMapping("/brands/list")
    // 这里id是必须要有的，这个注解默认也是必须要有这个id
    public R brandsList(@RequestParam(value = "catId") Long catId) {
        List<CategoryBrandRelationEntity> entities = categoryBrandRelationService.getBrandByCatId(catId);
        List<CategoryBrandRalationResponseVo> data = entities.stream().map((entity) -> {
            CategoryBrandRalationResponseVo responseVo = new CategoryBrandRalationResponseVo();
            BeanUtils.copyProperties(entity, responseVo);
            return responseVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", data);
    }
}
